package vsy.buzzwordbingo.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.View;

import vsy.buzzwordbingo.model.Game;
import vsy.buzzwordbingo.model.GameBoard;
import vsy.buzzwordbingo.model.GameCore;
import vsy.buzzwordbingo.model.Player;
import vsy.buzzwordbingo.model.impl.PlayerImpl;

@Controller
public class GameController {

	@Resource
	private GameCore gameCore;
	
	@Resource
	private Long ajaxGamePollInterval;

	@RequestMapping(value = "/game/{gameId}", method = RequestMethod.GET)
	public ModelAndView getGame(HttpServletRequest request,
			HttpServletResponse response, @PathVariable String gameId) {
		String playerName = request.getUserPrincipal().getName();
		
		GameBoard board = gameCore.joinGame(gameId, playerName);
		List<List<Buzzword>> buzzwords = new ArrayList<List<Buzzword>>();
		for (int row = 0; row < board.getSize(); row++) {
			List<Buzzword> rowList = new ArrayList<Buzzword>(); 
			for (int col = 0; col < board.getSize(); col++) {
				Buzzword buzzword = new Buzzword(board.getBuzzword(row, col), board.isMarked(row, col));
				rowList.add(buzzword);
			}
			buzzwords.add(rowList);
		}
		
//		if (game == null) {
//			return createEmptyResponse("text/html", HttpStatus.NOT_FOUND);
//		}
		
		Player player = gameCore.getPlayer(playerName);
		request.setAttribute("player", player);
		request.setAttribute("game", board.getGame());
		request.setAttribute("board", board);
		request.setAttribute("buzzwords", buzzwords);
		request.setAttribute("pollInterval", ajaxGamePollInterval);
		return new ModelAndView("game");
	}
	
	public static class Buzzword {
		private String buzzword;
		private boolean marked; 

		public Buzzword(String buzzword, boolean marked) {
			this.buzzword = buzzword;
			this.marked = marked;
		}
		public String getBuzzword() {
			return buzzword;
		}
		public boolean isMarked() {
			return marked;
		}
	}
	
	
	@RequestMapping(value = "/json/games/{gameId}/players", method = RequestMethod.DELETE)
	public ModelAndView deletePlayerFromGame(HttpServletRequest request,
			HttpServletResponse response, final @PathVariable String gameId) {

		System.out.println("delete Player");
		String playerName = request.getUserPrincipal().getName();
		gameCore.leaveGame(gameId, playerName);
		
		return new ModelAndView(new View() {

			@Override
			public String getContentType() {
				return "application/json";
			}

			@Override
			public void render(Map<String, ?> model,
					HttpServletRequest request, HttpServletResponse response)
					throws Exception {
			    response.setStatus(HttpStatus.OK.value());
			}
		});
	}
}